﻿/**********************  此代码是由代码生成器生成，生成时间： 2012/12/4 21:29:00  ************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;

using CRM.Entity;
using CRM.DAL.SQLiteHelper;
using System.Data.SQLite;

namespace CRM.DAL
{
    public partial class TextInfoDAL
    {
        public bool TextInfo_Add(TextInfo textInfo)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("insert into [TextInfo]");
            sb.Append("(");
            sb.Append("TextKey,");
            sb.Append("TextValue");
            sb.Append(")");
            sb.Append("values");
            sb.Append("(");
            sb.Append("@TextKey,");
            sb.Append("@TextValue");
            sb.Append(");select last_insert_rowid()");

            SQLiteParameter[] sqlParameter = new SQLiteParameter[]
            {
                   new SQLiteParameter("@TextKey",textInfo.TextKey),
                   new SQLiteParameter("@TextValue",textInfo.TextValue),
            };

            try
            {
               var identity = Convert.ToInt32(DbHelperSQL.ExecuteScalar(sb.ToString(), sqlParameter));

                if (identity > 0)
                {
                    textInfo.TextId = identity;
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }

        }

        public bool TextInfo_Update(TextInfo textInfo)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("update [TextInfo] set ");
            sb.Append("TextKey=@TextKey,");
            sb.Append("TextValue=@TextValue");
            sb.Append(" where TextId=@TextId");
            
            SQLiteParameter[] parameters = 
            {
                  new SQLiteParameter("@TextId",textInfo.TextId),
                  new SQLiteParameter("@TextKey",textInfo.TextKey),
                  new SQLiteParameter("@TextValue",textInfo.TextValue),
            };

            try
            {
              var rows=  DbHelperSQL.ExecuteSql(sb.ToString(), parameters);
              
              if(rows>0)
                return true;
              return false;
              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }

        public bool TextInfo_Remove(TextInfo textInfo)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from [TextInfo] ");
            sb.Append(" where TextId=@TextId");
            SQLiteParameter[] parameters =
            {
                new SQLiteParameter("@TextId",textInfo.TextId)
            };

            try
            {
              var rows=DbHelperSQL.ExecuteSql(sb.ToString(), parameters);
              if(rows>0)
                return true;
              return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine("当前时间:{0}，数据编号:{1}删除异常！", DateTime.Now, textInfo.TextId);
                throw;
            }
        }
        
        public bool TextInfo_RemoveBatch(string ids)
        {
            if (string.IsNullOrEmpty(ids))
                return true;

            StringBuilder sb = new StringBuilder();

            sb.Append("delete from [TextInfo] ");
            sb.Append(" where TextId in (" + ids + ")");

            try
            {
                int rows = DbHelperSQL.ExecuteSql(sb.ToString());

                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        
        public List<TextInfo> TextInfo_List(List<Int64> ids)
        {
            var idList = string.Join(",", ids);

            var sql = "select * from [TextInfo] where TextId in (" + idList + ")";

            var table = DbHelperSQL.Query(sql).Tables[0];

            List<TextInfo> list = new List<TextInfo>();

            for (int i = table.Rows.Count - 1; i >= 0; i--)
            {
                list.Add(new TextInfo(table.Rows[i]));
            }

            return list;
        }
        
        public TextInfo TextInfo_Single(Int64 id)
        {
            var sql = "select * from [TextInfo] where TextId=" + id;

            var table = DbHelperSQL.Query(sql).Tables[0];

            return new TextInfo(table.Rows[0]);
        }
    }
}
